from fastapi import APIRouter, Depends, Query
from application.logic import NoteLogic
from fastapi.responses import JSONResponse
from application.entity.NoteEntity import NoteAdd, NoteEdit
from application.util.ResponseUtil import ResponseUtil
from application.dependency.TokenDependency import verify_token

# 设置路由
router: APIRouter = APIRouter(prefix="/note", tags=["帖子"])


@router.post("/add", summary="新增帖子")
async def add_note(note_add: NoteAdd, user_id: int = Depends(verify_token)) -> JSONResponse:
    await NoteLogic.add_note(user_id=user_id, note_add=note_add)
    return ResponseUtil().success()


@router.post("/delete/{note_id:int}", summary="根据ID删除帖子")
async def delete_note_by_id(note_id: int, user_id: int = Depends(verify_token)) -> JSONResponse:
    await NoteLogic.delete_note_by_id(note_id=note_id, user_id=user_id)
    return ResponseUtil().success()


@router.get("/info/{note_id:int}", summary="根据ID获取帖子", dependencies=[Depends(verify_token)])
async def get_note_by_id(note_id: int) -> JSONResponse:
    return ResponseUtil(data=await NoteLogic.get_note_by_id(note_id=note_id)).success()


@router.get("/list/{user_id:int}", summary="根据用户ID获取帖子列表", dependencies=[Depends(verify_token)])
async def get_list_by_user_id(user_id: int, page: int = Query(default=1, ge=1),
                              page_size: int = Query(default=10, ge=1)) -> JSONResponse:
    note_list: list = await NoteLogic.get_list_by_user_id(user_id=user_id, page=page, page_size=page_size)
    return ResponseUtil(data=note_list).success()


@router.post("/edit", summary="根据ID编辑帖子")
async def edit_note_by_id(note_edit: NoteEdit, user_id: int = Depends(verify_token)) -> JSONResponse:
    await NoteLogic.edite_note_by_id(note_edit=note_edit, user_id=user_id)
    return ResponseUtil().success()
